package com.groupseven.happycampus.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.groupseven.happycampus.common.bo.BicycleOrderBo;
import com.groupseven.happycampus.common.instener.WebOrderListener;
import com.groupseven.happycampus.common.pojo.BicycleOrder;
import com.groupseven.happycampus.common.result.R;
import com.groupseven.happycampus.common.vo.LayuiPageVo;
import com.groupseven.happycampus.service.BicycleOrderService;
import jdk.nashorn.internal.ir.ReturnNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.ReactiveTransaction;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/bicycleOrder")
public class BicycleOrderController {
    @Autowired
    private BicycleOrderService bicycleOrderService;

    @RequestMapping("orderList")
    public LayuiPageVo<BicycleOrder> bicycleOrderList(@RequestParam Map<String, Object> params) {
        System.out.println(params);
        return bicycleOrderService.bicycleOrderListPageData(params);
    }

    @RequestMapping("/bicycleOrderAdd")
    public R bicycleOrderAdd(BicycleOrder bicycleOrder) {
        int result = bicycleOrderService.bicycleOrderAdd(bicycleOrder);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping("/bicycleOrderUpdate")
    public R bicycleOrderUpdate(BicycleOrder bicycleOrder) {
        int result = bicycleOrderService.bicycleOrderUpdate(bicycleOrder);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping("/bicycleOrderDelete")
    public R bicycleOrderDelete(@RequestParam("orderId") Integer id) {
        System.out.println("传进来的id" + id);
        int result = bicycleOrderService.bicycleOrderDelete(id);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @Autowired
    private WebOrderListener webOrderListener;

    @RequestMapping("/orderExport")
    public void orderExport(BicycleOrder bicycleOrder, HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String filename = URLEncoder.encode("订单信息导出", "utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + filename + ".xlsx");
        ServletOutputStream outputStream = response.getOutputStream();
        ExcelWriterBuilder write = EasyExcel.write(outputStream, BicycleOrder.class);
        ExcelWriterSheetBuilder sheet = write.sheet();
        List<BicycleOrderBo> bicycleOrderBoList = bicycleOrderService.orderExcelData(bicycleOrder);
        sheet.doWrite(bicycleOrderBoList);

    }

    @RequestMapping("/orderImport")
    public R orderImport(MultipartFile file) throws IOException {
        if (file != null) {
            ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), BicycleOrderBo.class, webOrderListener);
            // 得到 sheet 对象
            ExcelReaderSheetBuilder sheet = read.sheet();
            // 读取sheet
            sheet.doRead();
        }
        return R.ok();
    }


}
